#include "enemy.h"

enemy::enemy() 
	: mActive(false)
{
}

enemy::~enemy()
{
}


void enemy::Load()
{
	mSprite.Load("pikachu.png");
}

void enemy::Unload()
{
	mSprite.Unload();
}

void enemy::Update(float deltaTime)
{
	if (mActive)
	{
		mPosition += mVelocity * deltaTime;

		enemyDelay -= deltaTime;

		if (enemyDelay <=0)
		{

			SVector2 newVel = mVelocity;
			newVel.x = -newVel.x;

			mVelocity = newVel;

			enemyDelay = RandomFloat (0.7f, 0.3f);

		}
	}

	const int kWinWidth = IniFile_GetInt("WinWidth", 800);
	const int kWinHeight = IniFile_GetInt("WinHeight", 600);
	const int kThreshold = mSprite.GetWidth();
	if (mPosition.x < -kThreshold || mPosition.x > kWinWidth + kThreshold||
		mPosition.y < -kThreshold || mPosition.y > kWinHeight + kThreshold)
	{
		Kill();
	}
}



void enemy::Render()
{
	if (mActive)
	{
		int width = mSprite.GetWidth();
		int height =mSprite.GetHeight();
		SVector2 offset(width * 0.5f, height * 0.5f);
		SVector2 renderPosition = mPosition - offset;
		mSprite.SetPosition(renderPosition);
		mSprite.Render();
	}
}

void enemy::Spawn(const SVector2& pos)
{

	mPosition = pos;
	mVelocity = SVector2(200.0f, 200.0f);
	mActive = true;
}

void enemy::Kill()
{
	mActive = false;
}
